//
//  ViewController.m
//  Homework_17(2)
//
//  Created by 徐昊 on 16/8/6.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "CustomTableViewCell.h"
#import "ContactViewController.h"
#import "AddViewController.h"

#define kTableViewCellName @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, addContactDelegate>
@property (nonatomic, strong) NSMutableArray *arrOfModel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.修改数据源
    [_arrOfModel removeObjectAtIndex:indexPath.row];
    // 2.对应修改cell
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}
- (void)handleData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
    NSLog(@"%@", array);
    self.arrOfModel = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in array) {
        NSLog(@"%@", dic);
        Contact *contact = [[Contact alloc] init];
        // model赋值 (KVC)
        [contact setValuesForKeysWithDictionary:dic];
        // model 放入新数组中
        [self.arrOfModel addObject:contact];
    }
    NSLog(@"%ld", _arrOfModel.count);
//    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSLog(@"%@", [pathArray firstObject]);
//    NSString *contactPath = [[pathArray firstObject] stringByAppendingPathComponent:@"contact.plist"];
//    BOOL isSuccess = [NSKeyedArchiver archiveRootObject:_arrOfModel toFile:contactPath];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrOfModel.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellName];
    cell.name.text = [_arrOfModel[indexPath.row] name];
    cell.phoneNumber.text = [_arrOfModel[indexPath.row] phoneNumber];
    return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([sender isKindOfClass:[CustomTableViewCell class]]) {
        CustomTableViewCell *cell = sender;
        ContactViewController *contact = segue.destinationViewController;
        NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
        contact.contact = _arrOfModel[indexPath.row];
    } else {
        AddViewController *add = segue.destinationViewController;
        add.delegate = self;
    }
}
// 协议方法
- (void)addContact:(Contact *)contact {
    if ((contact.name != nil) && (contact.phoneNumber != nil)) {
        [_arrOfModel addObject:contact];
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
